package zhuyun.code.horizontalscrollview

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.widget.ListView
import kotlin.math.abs

/**
 * @author: Administrator
 * @date: 2022/1/1
 */
class MyListView(context: Context,attributeSet: AttributeSet?,defStyleAttr:Int): ListView(context,attributeSet,defStyleAttr) {
    constructor(context: Context,attributeSet: AttributeSet?):this(context,attributeSet,0)
    constructor(context: Context):this(context,null)

    private var currentX = 0
    private var currentY = 0
    private var lastX = 0
    private var lastY = 0



    //内部拦截法
    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        currentX = ev!!.x.toInt()
        currentY = ev!!.y.toInt()
        when(ev!!.action){
            MotionEvent.ACTION_DOWN->{
                parent.requestDisallowInterceptTouchEvent(true)
            }
            MotionEvent.ACTION_MOVE->{
                val dx = currentX - lastX
                val dy = currentY - lastY
                if(abs(dx) > abs(dy)){
                    parent.requestDisallowInterceptTouchEvent(false)
                }
            }
            MotionEvent.ACTION_UP->{

            }
        }
        lastX = currentX
        lastY = currentY
        return super.dispatchTouchEvent(ev)
    }
}